package com.elm.cart.service.impl;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.elm.api.client.BusinessClient;
import com.elm.api.client.FoodClient;
import com.elm.cart.mapper.CartMapper;
import com.elm.cart.pojo.Cart;
import com.elm.cart.service.CartService;
import com.elm.common.pojo.Business;
import com.elm.common.pojo.Food;
import com.elm.common.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private BusinessClient businessClient;
    @Autowired
    private FoodClient foodClient;
    @Override
    public List<Cart> listCart(String userId, Integer businessId) {
        List<Cart> list=cartMapper.listCart(userId,businessId);
        for (Cart cart : list) {
            Integer bId = cart.getBusinessId();
            Integer foodId = cart.getFoodId();
            Result<Business> result = businessClient.getBusinessById(bId);
            Result<Food> result2 = foodClient.findFoodByFoodId(foodId);
            cart.setFood(result2.getData());
            cart.setBusiness(result.getData());
        }
        return list;
    }

    @Override
    public Integer saveCart(Cart cart) {
        return cartMapper.saveCart(cart);
    }

    @Override
    public Integer update(Cart cart) {
        return cartMapper.update(cart);    }

    @Override
    public Integer removeCart(String userId, Integer businessId, Integer foodId) {
        return cartMapper.removeCart(userId,businessId,foodId);
    }
}
